package com.yupi.springbootinit.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.springbootinit.annotation.AuthCheck;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.DeleteRequest;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.exception.ThrowUtils;
import com.yupi.springbootinit.model.dto.chart.ChartAddRequest;
import com.yupi.springbootinit.model.dto.chart.ChartEditRequest;
import com.yupi.springbootinit.model.dto.chart.ChartQueryRequest;
import com.yupi.springbootinit.model.dto.chart.ChartUpdateRequest;
import com.yupi.springbootinit.model.entity.Chart;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.service.ChartService;
import com.yupi.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Blob;
import java.util.Currency;

@RestController
@RequestMapping("/chart")
@Slf4j
public class ChartController {

    public static void main(String[] args) {
        Boolean flag=false;
        ThrowUtils.throwIf(!flag,ErrorCode.OPERATION_ERROR);
    }

    @Autowired
    private ChartService chartService;

    @Autowired
    private UserService userService;

    /*
    * 新增
    * @param chartAddRequest
    * @param request
    * return ResultUtils
    * */
    @PostMapping("addChart")
    public BaseResponse<Long> addChart(@RequestBody ChartAddRequest chartAddRequest,
                                       HttpServletRequest request) {
        if (chartAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Chart chart = new Chart();
        BeanUtils.copyProperties(chartAddRequest, chart);
        User loginUser = userService.getLoginUser(request);
        chart.setUserId(loginUser.getId());
        boolean save = chartService.save(chart);
        ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
        Long newChartId = chart.getId();
        return ResultUtils.success(newChartId);
    }

    /*
     * 删除
     * @param chartDelRequest
     * @param request
     * return ResultUtils
     * */
    @PostMapping("delete")
    public BaseResponse<Boolean> deleteChart(@RequestBody DeleteRequest deleteRequest,
                                             HttpServletRequest request){
        if(deleteRequest==null||deleteRequest.getId()<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        Long id = deleteRequest.getId();
        Chart oldChart = chartService.getById(id);
        ThrowUtils.throwIf(oldChart==null,ErrorCode.NOT_FOUND_ERROR);
        if(!oldChart.getUserId().equals(id)&& !userService.isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean result = chartService.removeById(deleteRequest.getId());

        return ResultUtils.success(result);
    }

    /*
    *更新(仅管理员)
    * @param chartUpdateRequest
    * return ResultUtils
    * */

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse updateChart(@RequestBody ChartUpdateRequest chartUpdateRequest){
       if(chartUpdateRequest==null||chartUpdateRequest.getId()<=0){
           throw new BusinessException(ErrorCode.PARAMS_ERROR);
       }
        Chart chart = new Chart();
        BeanUtils.copyProperties(chartUpdateRequest,chart);
        Long id = chartUpdateRequest.getId();
        Chart oldChart = chartService.getById(id);
        ThrowUtils.throwIf(oldChart==null,ErrorCode.NOT_FOUND_ERROR);
        boolean result = chartService.updateById(chart);
        return ResultUtils.success(result);
    }

    /*
    * 根具id获取
    * @param id
    * return
    * */
    @GetMapping("/get")
    public BaseResponse<Chart> getChartById(Long id,
                                            HttpServletRequest request){
        if(id<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Chart chart = chartService.getById(id);
        ThrowUtils.throwIf(chart==null,ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(chart);
    }

    /*
    * 分页获取图表信息
    * @param ChartQueryRequest
    * */
    @GetMapping("listChartByPage")
    public BaseResponse<Page<Chart>> listChartByPage(ChartQueryRequest chartQueryRequest){
        long current = chartQueryRequest.getCurrent();
        long size = chartQueryRequest.getPageSize();
        ThrowUtils.throwIf(size>20,ErrorCode.PARAMS_ERROR);
        Page<Chart> page = chartService.page(new Page<>(current, size));
        return ResultUtils.success(page);

    }

    /*
    * 获取当前用户的图标信息
    * @param
    * @return
    * */
    @GetMapping("listMyChartByPage")
    public BaseResponse<Page<Chart>>  listMyChartByPage(ChartQueryRequest chartQueryRequest,
                                                        HttpServletRequest request){
        if(chartQueryRequest==null || chartQueryRequest.getId()<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",loginUser.getId());
        long current = chartQueryRequest.getCurrent();
        long pageSize = chartQueryRequest.getPageSize();
        Page<Chart> page = new Page<>(current,pageSize);
        Page<Chart> result = chartService.page(page, queryWrapper);
        return ResultUtils.success(result);
    }

    /*
    * 编辑
    * @param ChartEditRequest
    * return
    * */
    @PostMapping("editChart")
    public BaseResponse<Boolean> editChart(@RequestBody ChartEditRequest chartEditRequest,
                                        HttpServletRequest request){
           if(chartEditRequest==null ||chartEditRequest.getId()<=0){
               throw new BusinessException(ErrorCode.PARAMS_ERROR);
           }
           Chart chart = new Chart();
           BeanUtils.copyProperties(chartEditRequest,chart);
           User loginUser = userService.getLoginUser(request);
        //判断是否是同一个人查询
           Long id = chartEditRequest.getId();
           Chart oldChart = chartService.getById(id);
           ThrowUtils.throwIf(oldChart==null,ErrorCode.NOT_FOUND_ERROR);
           if(!oldChart.getUserId().equals(loginUser.getId())&&!userService.isAdmin(loginUser)){
              throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
           }
           boolean result = chartService.updateById(chart);
           return ResultUtils.success(result);
    }
}
